package com.bfd.es_demo.controller;

import com.alibaba.fastjson.JSON;
import com.bfd.es_demo.config.ElasticsearchUtil;
import com.bfd.es_demo.pojo.Movie;
import com.bfd.es_demo.service.MoviceService;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * @author:yong.li
 * @date:2019/10/21 14:01
 */
@Controller
public class MovieController {
    @Autowired
    private MoviceService moviceService;


    @ResponseBody
    @RequestMapping(value = "/movie",method = RequestMethod.GET)
    @ApiOperation(value = "所有电影信息",notes = "movie is my love")
    public List<Movie> getAll(){
        return moviceService.getAll();
    }

    @ResponseBody
    @RequestMapping(value = "/movietoes",method = RequestMethod.GET)
    @ApiOperation(value = "所有电影信息",notes = "movie is my love")
    public Boolean moviesToES() {
        List<Movie> movies = moviceService.getAll();
        for (Movie m: movies
             ) {
            JSONObject jo = new JSONObject();
            JSONObject obj= JSON.parseObject(jo.toJSONString(m));
            System.out.print(obj);
            try {
                String id = ElasticsearchUtil.addData("deep","movies",m.getmId(),obj);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return true;
    }
    //https://blog.csdn.net/paditang/article/details/78802799
}
